using System;
using System.Collections;
using System.Xml;

namespace Slaks.DocumentSerialization
{
	public class ParsedFiles
	{
		private ArrayList m_parsedFileNames;

		public ParsedFiles() { m_parsedFileNames = new ArrayList(); }

		public IList Files
		{
			get { return m_parsedFileNames; }
		}

		public void XmlWrite(string path)
		{
			XmlTextWriter writer = null;
			
			try
			{
				writer = new XmlTextWriter(path,System.Text.Encoding.ASCII);
				writer.Formatting = System.Xml.Formatting.Indented;
				writer.WriteStartDocument();
				writer.WriteStartElement(Grammar.ParsedFiles.ParsedFilesElement);
				foreach(string filename in m_parsedFileNames)
				{
					writer.WriteElementString(Grammar.ParsedFiles.FileElement,filename);
				}
				writer.WriteEndElement();
				writer.WriteEndDocument();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			finally
			{
				writer.Close();
			}
		}

		/// <summary>
		/// Read the xml file with file elements 
		/// </summary>
		/// <param name="path"></param>
		/// <returns></returns>
		public static IList ReadXml(string path)
		{
			XmlReader reader = null;
			IList list = null;
			try
			{
				list = new ArrayList();
				reader = new XmlTextReader(path);
				string file;

				while(reader.Read())
				{
					if (reader.NodeType != XmlNodeType.Element) continue;
					if (string.Equals(reader.Name,Grammar.ParsedFiles.FileElement))
					{
						file = reader.ReadString();
						if (file != String.Empty) list.Add(file);
					}
				}
			}catch(Exception ex)
			{
				throw ex;
			}
			finally
			{
        if (reader != null) reader.Close();
			}
			return list;
		}
	}
}
